package dk.marten.ibdrecord;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.res.Resources;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class RedigerToilet extends Activity implements OnSeekBarChangeListener {

private SeekBar sliderKonsistens, sliderSlim, sliderBlod, sliderTrang;
	
	private TextView  konsistensValue, slimValue, blodValue, trangValue, datoTekst, tidsTekst;
	
	private DBAccess dBAccess;

	private ToiletData toilet;	

	private String[] konsistensSteps;
	private String[] generelSteps;
	private String[] blodSlimSteps;
	private long id = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rediger_toilet);
		
		
		
		if (getIntent().hasExtra("id")){
			id = getIntent().getExtras().getLong("id");
		}
		
		dBAccess = new DBAccess(this);

		konsistensValue = (TextView) findViewById(R.id.konsistensValue);
		slimValue = (TextView) findViewById(R.id.slimValue);
		blodValue = (TextView) findViewById(R.id.blodValue);
		trangValue = (TextView) findViewById(R.id.trangValue);
		datoTekst = (TextView) findViewById(R.id.datoTekst);
		tidsTekst = (TextView) findViewById(R.id.tidsTekst);
		
		sliderKonsistens = (SeekBar) findViewById(R.id.sliderKonsistens);
		sliderSlim = (SeekBar) findViewById(R.id.sliderSlim);
		sliderBlod = (SeekBar) findViewById(R.id.sliderBlod);
		sliderTrang = (SeekBar) findViewById(R.id.sliderTrang);

		sliderKonsistens.setOnSeekBarChangeListener(this);
		sliderSlim.setOnSeekBarChangeListener(this);
		sliderBlod.setOnSeekBarChangeListener(this);
		sliderTrang.setOnSeekBarChangeListener(this);
		
		Resources res = getResources();
		konsistensSteps = res.getStringArray(R.array.konsistensSteps);
		generelSteps = res.getStringArray(R.array.generelSteps);
		blodSlimSteps = res.getStringArray(R.array.blodSlimSteps);

		toilet = new ToiletData();
		
		if(id != -1){
			toilet = dBAccess.getToilet(id);
		}

		sliderKonsistens.setProgress(toilet.getKonsistens());
		sliderSlim.setProgress(toilet.getSlim());
		sliderBlod.setProgress(toilet.getBlod());
		sliderTrang.setProgress(toilet.getTrang());	
		
		datoTekst.setText(DateFormat.format("dd/MM/yyyy", toilet.getDato()));
		tidsTekst.setText(DateFormat.format("h:mm", toilet.getDato()));	

		updateSeekbars();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_rediger_toilet, menu);

		if(id != -1){
			MenuItem btn = (MenuItem) menu.getItem(2);
			if(btn != null) btn.setVisible(false);
			Log.d("MOE",btn.getItemId() +" : "+R.id.redToiletDeleteBtn);
		}
		
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.d("MOE","onOptionsItemSelected");

		switch(item.getItemId()){
		case R.id.redToiletGemBtn:
			gemData();
			break;
		case R.id.redToiletCancelBtn:
			finish();
			break;
		case R.id.redToiletDeleteBtn:
			break;
		}
        return true;
    }
	
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    	switch(seekBar.getId()){
	    	case R.id.sliderKonsistens:
	    	case R.id.sliderTrang:
	    	case R.id.sliderSlim:
	    	case R.id.sliderBlod:
	    		updateSeekbars();
	    	break;
    	}
    }
    
    private void updateSeekbars(){
    	konsistensValue.setText(konsistensSteps[sliderKonsistens.getProgress()]);
    	slimValue.setText(blodSlimSteps[sliderSlim.getProgress()]);
    	blodValue.setText(blodSlimSteps[sliderBlod.getProgress()]);
    	trangValue.setText(generelSteps[sliderTrang.getProgress()]);
    }
    
	public void redigerDato(View view) {
		showDialog(1);
	 }
	    
	 public void redigerTid(View view) {
		showDialog(2);
	 }
	 
	 public void gemData(View view) {
		 gemData();
	 }
	    
	 public void gemData() {
		 toilet.setSlim(sliderSlim.getProgress());
		 toilet.setBlod(sliderBlod.getProgress());
		 toilet.setTrang(sliderTrang.getProgress());
		 toilet.setKonsistens(sliderKonsistens.getProgress());

		 dBAccess.open();
		 dBAccess.insertOrUpdateToilet(toilet);
		 dBAccess.close();
		 this.finish();
	 }

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Calendar regDato = toilet.getDato();
		switch (id) {
		case 1:
			return new DatePickerDialog(this, datePickerListener, regDato.get(Calendar.YEAR), regDato.get(Calendar.MONTH),regDato.get(Calendar.DAY_OF_MONTH));
		case 2:
			return new TimePickerDialog(this, timePickerListener, regDato.get(Calendar.HOUR_OF_DAY), regDato.get(Calendar.MINUTE),false);
		}
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 
		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int year, int month, int day) {
			Calendar regDato = toilet.getDato();
			regDato.set(Calendar.YEAR, year);
			regDato.set(Calendar.MONTH, month);
			regDato.set(Calendar.DAY_OF_MONTH, day);
			datoTekst.setText(DateFormat.format("dd/MM/yyyy", regDato));
			toilet.setDato(regDato);
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {	
			Calendar regDato = toilet.getDato();
			regDato.set(Calendar.HOUR_OF_DAY, hourOfDay);
			regDato.set(Calendar.MINUTE, minute);
			tidsTekst.setText(DateFormat.format("h:mm", regDato));
			toilet.setDato(regDato);
		}
	};
	
}
